// Learn cc.Class:
//  - https://docs.cocos.com/creator/manual/en/scripting/class.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

cc.Class({
    extends: cc.Component,
    ctor(){
        //360° / 秒
        this.degSpeed = 360;
        this.startDeg = 90;
        this.endDeg = -270;
        this.FPS = 60;
        this.isStart = false;
        this._curSeconds = 3;
    },
    properties: {
        //倒计时 圈，画笔
        countDownGrpahics: cc.Graphics,
        //倒计时 Label  3,2,1
        countDownLabel: cc.Label,
        //成功，失败背景
        contBg: cc.Animation,
        //返回列表
        goBackBtn: cc.Node,
        //继续游戏
        toStartBtn: cc.Node,
        //关闭按钮
        closeBtn: cc.Node,
        //乐豆：
        douBox: cc.Node,
        //得分
        scoreBox: cc.Node,
        //卡
        cardBox: cc.Node,
        //乐豆数量
        douLabel: cc.Label,
        //得分label
        scoreLabel: cc.Label,
        //卡名
        cardNameLabel: cc.Label,
        //卡的数量
        cardNumLabel: cc.Label,
        //倒计时
        curSeconds: {
            get: function(){
                return this._curSeconds
            },
            set: function(val){
                this._curSeconds = val;
                this.countDownLabel.string = this._curSeconds;
            }
        },
        //游戏状态
        gameState: {
            //0：游戏失败， 1：游戏成功
            set: function(val){
                switch(val){
                    case 1:
                        this.contBg.play('gameOverSucc')
                        break;
                    case 0:
                        this.contBg.play('gameOverFail')
                        break;
                }
                this.setLayout(val);
            }
        }
    },
    // LIFE-CYCLE CALLBACKS:
    onLoad () {
        this.init();
        //关闭按钮
        this.closeBtn.on('touchstart', this.closeGameOver, this)
        //继续游戏
        this.toStartBtn.on('touchstart', this.toStart, this)
        //返回列表
        this.goBackBtn.on('touchstart', this.backList, this)
        //游戏结束
        this.node.on('gameover', (obj)=>{
            this.init();
            /*
                score: this.score,
                bean: this.getXLbean,
                card: {
                    text: '金卡',
                    num: 1
                }            
            */
            //设置得分
            this.scoreLabel.string = String(obj.score);
            //设置得豆
            this.douLabel.string = String(obj.bean);
            if(obj.bean<0){
                this.gameState = 0;
            }else{
                this.gameState = 1;
            }
            this.cardNameLabel.string = '得到'+obj.card.text;
            this.cardNumLabel.string = String(obj.card.num);
            this.startCountDown();
        })
    },
    init(){
        this.startDeg = 90;
        this.endDeg = -270;
        this.curSeconds = 3;
    },
    //设置布局
    setLayout(status){
        if(status == 0){
            this.cardBox.active = false;
            this.douBox.x = -105;
            this.scoreBox.x = 105;
        }else if(status == 1){
            this.cardBox.active = true;
            this.douBox.x = -150;
            this.scoreBox.x = 0;
            this.cardBox.x = 150;
        }
    },
    //返回列表
    backList(){
        try {
            // ios 方式
            window.webkit.messageHandlers.goBack.postMessage(null);
        } catch (e1) {
            try {
                // android 方式
                window.joybuy.goBackAction();
            } catch (e2) {
                history.go(-1)
            }
        }
    },  
    //点击继续游戏
    toStart(){
        this.isStart = false;
        this.node.emit('restart')
        this.node.active = false;
    },
    //时间到了自动继续游戏
    closeGameOver(){
        this.isStart = false;
        this.node.emit('close')
        this.node.active = false;
    },
    //开始倒计时
    startCountDown(){
        this.isStart = true;
    },
    start () {

    },
    update (dt) {
        this.FPS = 1/dt;
        this.countDownGrpahics.clear();
        this.countDownGrpahics.arc(0,0,78, this.startDeg*Math.PI/180, this.endDeg*Math.PI/180)
               
        if(this.isStart){
            this.endDeg+=this.degSpeed/this.FPS;
            if(this.endDeg >= this.startDeg){
                this.endDeg = -270;
                this.curSeconds -= 1;
                if(this.curSeconds == 0){
                    this.isStart = false;
                    this.toStart();
                }
            }
        }
        this.countDownGrpahics.stroke(); 
    },
});
